package com.koro.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.koro.cache.RedisService;
import com.koro.dao.UsersRepository;
import com.koro.dto.SaveUsersDTO;
import com.koro.entity.LoginUser;
import com.koro.entity.Users;
import com.koro.mapper.UsersMapper;
import com.koro.service.UsersService;
import com.koro.tool.SecurityJwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private AuthenticationManager authenticationManager;
    //   @Autowired
    //   private SecurityJwtUtil securityJwtUtil;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private RedisService redisService;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UsersRepository usersRepository;

    @Override
    public String login(Users users) {
        /*String script = "";
        DefaultRedisScript<Object> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptText(script);
        redisScript.setResultType(Object.class);

        List<Object> list = new ArrayList<>();
        redisTemplate.execute(redisScript,list,String.valueOf(users.getId()));

        return null;*/

        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken
                = new UsernamePasswordAuthenticationToken(users.getUsername(),users.getPassword());
        Authentication authenticate = authenticationManager.authenticate(usernamePasswordAuthenticationToken);
        if(authenticate == null) {
            throw new RuntimeException("The username or password is wrong.");
        }
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String token = SecurityJwtUtil.sign(loginUser.getUsername(), "PC");
        return "Bearer" + token;
    }

    @Override
    public Integer save(SaveUsersDTO dto) {
        Users users = new Users();
        BeanUtils.copyProperties(dto,users);
        if(!"".equals(dto.getId()) || dto.getId() == 0) {
            users.setPassword(passwordEncoder.encode(users.getPassword()));
            usersMapper.insert(users);
        } else {
            usersMapper.updateById(users);
        }
        usersRepository.save(users);
        return users.getId();
    }

    @Override
    public void synGoodsES() {
        List<Users> list = usersMapper.selectList(new QueryWrapper<Users>());
        list.forEach(u -> usersRepository.save(u));
    }


}
